package com.syni.mdd.yhd.common.all.listen.video;

import cn.hutool.core.collection.CollUtil;
import com.syni.mdd.sjb.common.component.utils.common.utils.StringUtils;
import com.syni.mdd.yhd.common.all.constants.AuroraPushCode;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.entity.BmsDxNews;
import com.syni.mdd.yhd.common.all.entity.BmsUserVideo;
import com.syni.mdd.yhd.common.all.entity.BmsVideo;
import com.syni.mdd.yhd.common.all.entity.user.BmsUser;
import com.syni.mdd.yhd.common.all.service.BmsCommonService;
import com.syni.mdd.yhd.common.all.service.BmsDxNewsService;
import com.syni.mdd.yhd.common.all.service.BmsUserVideoService;
import com.syni.mdd.yhd.common.all.utils.TokenManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @className VideoLikeEventListener
 * @description TOO
 * @Author cfx
 * @DATE 2019/8/13 17:37
 * @VERSION 1.0
 **/
@Component
public class VideoLikeEventListener implements ApplicationListener<VideoEvent> {
    final static String MODEL = "videoLike";

    @Autowired
    BmsUserVideoService bmsUserVideoService;

    @Autowired
    BmsDxNewsService bmsDxNewsService;

    @Async
    public void onApplicationEvent(VideoEvent videoEvent) {
        String model = videoEvent.getSource().toString();
        if(!MODEL.equals(model)){
            return;
        }
        BmsVideo bmsVideo = videoEvent.getBmsVideo();
        Integer userId = videoEvent.getUserId();
        if(bmsVideo.getFromOrigin() == Constants.ADMIN_CLIENT){
            //后台发的视频不发送消息
            return;
        }
        BmsUserVideo bmsUserVideo = bmsUserVideoService.getByVideoId(bmsVideo.getId());
        if(bmsUserVideo == null){
            return;
        }
        //判断是否自己点赞
        if(bmsUserVideo.getUserId() == userId.intValue()){
            return;
        }
        BmsDxNews bmsDxNews = bmsDxNewsService.getBaseDao().findByBmsVideoIdAndParentTypeAndChildType(bmsVideo.getId(),
                BmsDxNewsService.PARENTTYPT_INT,BmsDxNewsService.PARENTTYPT_INT_LIKE);
        if(bmsDxNews == null){
            synchronized (this){
                bmsDxNews = bmsDxNewsService.getBaseDao().findByBmsVideoIdAndParentTypeAndChildType(bmsVideo.getId(),
                        BmsDxNewsService.PARENTTYPT_INT,BmsDxNewsService.PARENTTYPT_INT_LIKE);
                if(bmsDxNews == null){
                    //插入一条消息通知
                    bmsDxNews = new BmsDxNews();
                    bmsDxNews.setBmsVideoId(bmsVideo.getId());
                    bmsDxNews.setOpUserId(userId);
                    bmsDxNews.setParentType(BmsDxNewsService.PARENTTYPT_INT);
                    bmsDxNews.setChildType(BmsDxNewsService.PARENTTYPT_INT_LIKE);
                    bmsDxNews.setNewTime(new Date());
                    bmsDxNews.setUpdateTime(new Date());
                    bmsDxNews.setVideoName(bmsVideo.getVendorName());
                    bmsDxNews.setVideoImg(bmsVideo.getCloudFileSnapshot10Url());
                    //bmsDxNews.setFromOrigin(bmsVideo.USER_CLIENT);
                    bmsDxNews.setOriginRole(bmsVideo.getOriginRole());
                    bmsDxNews.setBmsUserId(bmsUserVideo.getUserId());
                    bmsDxNews.setOriginRole(bmsVideo.getOriginRole());
                    bmsDxNews.setBmsBusinessId(bmsVideo.getBmsId());
                }else {
                    bmsDxNews.setOpUserId(userId);
                }
            }
        }else {
            bmsDxNews.setOpUserId(userId);
        }
        bmsDxNewsService.save(bmsDxNews);
        //处理后置
        pushToFront(bmsDxNews);

    }

    public void pushToFront(BmsDxNews bmsDxNews){
        List<BmsDxNews> bmsDxNewsList = new LinkedList<>();
        bmsDxNewsList.add(bmsDxNews);
        bmsDxNewsService.handBmsDxNewsListLikeQue(bmsDxNewsList);
        //使用极光推送推送至前端
        String loginName = Constants.DEFAULT_USER_NAME;
        BmsUser bmsUser = TokenManager.getUser(bmsDxNews.getOpUserId());
        if(bmsUser != null && StringUtils.isNotBlank(bmsUser.getLoginName())){
            loginName = bmsUser.getLoginName();
        }
        String alert = String.format(AuroraPushCode.pushTemplateMap.get(AuroraPushCode.USER_DO_LIKE).getAlert(),loginName);
        BmsCommonService.pushToFront(bmsDxNews,AuroraPushCode.USER_DO_LIKE,alert);
    }
}
